import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { HistoryService } from "./history.service";
import { SingleComponent } from "../single/single.component";
import { FlowComponent } from "../flow/flow.component";
import * as _ from 'lodash';
import { DataService } from '../../../../../services/data.service';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { Util } from '../../../../../common/util';
@Component({
  selector: 'app-history',
  templateUrl: './history.component.html',
  styleUrls: ['./history.component.scss'],
  providers: [HistoryService]
})
export class HistoryComponent implements OnInit {
  // 查看单条基金变更模版
  @ViewChild("singles", { read: ViewContainerRef })
  singles: ViewContainerRef;
  singleComponent: ComponentRef<SingleComponent>;
  // 查看流程模版
  @ViewChild("flows", { read: ViewContainerRef })
  flows: ViewContainerRef;
  flowComponent: ComponentRef<FlowComponent>;
  // 用于识别指标类型
  public type: any;
  // 列表数据
  public dataList: any;
  // 查询
  public getBody: any = {
    pageSize: 50,
    pageIndex: 1,
    nIxIds: '',
    sBegin: '',
    sEnd: ''
  }
  //分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 50,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  constructor(
    public historyService: HistoryService,
    public resolver: ComponentFactoryResolver
  ) { }

  // 条数分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 列表分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  ngOnInit() {
    this.list();
  }
  // 获取历史变更
  list(pageNum?, pageSize?) {
    let that = this;
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.historyService.list(this.getBody).subscribe(data => {
      if (data) {
        that.dataList = data.list;
        console.log(data);
        this.listPageInfo.currentPageNum = data.pageNum;
        this.listPageInfo.totalPages = data.pages;
        this.listPageInfo.total = data.total;
        this.listPageInfo.startRow = data.startRow;
        this.listPageInfo.endRow = data.endRow;
      }
    })
  }
  // 查看单条基金变更模版
  showSingle(item) {
    if (this.singleComponent && this.singleComponent.instance) {
      this.singleComponent.destroy();
    }
    const singleComponent = this.resolver.resolveComponentFactory(SingleComponent);
    this.singleComponent = this.singles.createComponent(singleComponent);
    this.singleComponent.instance.getBody.nIxId = item.nIxId;
    this.singleComponent.instance.type = this.type;
    window['$']('#singles').modal('show');
  }
  // 查看流程状态模版
  showFlow(item) {
    if (this.flowComponent && this.flowComponent.instance) {
      this.flowComponent.destroy();
    }
    const flowComponent = this.resolver.resolveComponentFactory(FlowComponent);
    this.flowComponent = this.flows.createComponent(flowComponent);
    this.flowComponent.instance.getFlowBody.nIjId = item.nIjId;
    this.flowComponent.instance.getFlowBody.nIxId = item.nFundNo;
    this.flowComponent.instance.getFlowBody.nFlowId = item.nFlowId;
    this.flowComponent.instance.getBody.nIjId = item.nIjId;
    this.flowComponent.instance.getBody.nIxId = item.nFundNo;
    this.flowComponent.instance.getBody.nFlowId = item.nFlowId;
    this.flowComponent.instance.type = this.type;
    window['$']('#historyFlow').modal('show');
  }
}
